/*
 * @Author: xmily
 * @Date: 2022-03-16 17:27:54
 * @LastEditors: xmily
 * @LastEditTime: 2023-02-07 00:53:49
 * @FilePath: \glxxapi\routes\afterClasSrvComm.js
 * @Description:
 * 手机微信端及PC端通知API
 * Copyright (c) 2022 by xmily/mxthink.com.cn, All Rights Reserved.
 */
const router = require("koa-router")();
const siteBaseInfo = require("../models/sitebaseinfo.js");
const sitecfg = require("../cfg/siteconfig.js");
const base = require("../models/Fjxx.js");
const c2t = require("../models/AfterClassService/c2t.js");
const application = require("../models/AfterClassService/application.js");
const commMethod = require("../cfg/commMethod.js");
var term = "";
router.prefix("/afterClasSrvComm");

router.use(async (ctx, next) => {
  let termlist = await commMethod.getTermList();
  term = termlist.sort()[termlist.length - 1];
  await next();
});
//获取课后服务课程与教师的关系列表
router.post("/getallc2t", async (ctx, next) => {
  try {
    let res = await c2t.GetByQuery(
      {
        schoolname: sitecfg.schoolname,
        term: term,
      },
      null,
      {
        className: 1,
      }
    );
    ctx.body = {
      error: false,
      result: res,
    };
  } catch (err) {
    ctx.body = {
      error: true,
      message: err.message,
    };
  }
});

//更新指定教师与课后服务课程之间的关联
router.post("/updatec2t", async (ctx, next) => {
  let userinfo = ctx.request.token;
  let classname = ctx.request.body.classname;
  //先删除原来的关联数据
  try {
    //先删除原来的关联数据
    await c2t.UpdateManyByQuery(
      {
        schoolname: sitecfg.schoolname,
        term: term,
        teachers: { name: userinfo.accountname, id: userinfo.userid },
      },
      {
        $pull: {
          teachers: {
            name: userinfo.accountname,
            id: userinfo.userid,
          },
        },
      }
    );
    //再加入新的关联
    let res = await c2t.UpdateManyByQuery(
      {
        schoolname: sitecfg.schoolname,
        term: term,
        className: { $in: classname },
      },
      {
        $push: {
          teachers: {
            name: userinfo.accountname,
            id: userinfo.userid,
          },
        },
      }
    );
    ctx.body = {
      error: false,
      result: res,
    };
  } catch (err) {
    ctx.body = {
      error: true,
      message: err.message,
    };
  }
});

//获取当前用户课后服务课程与教师的关系列表
router.post("/getc2tbyuser", async (ctx, next) => {
  let userinfo = ctx.request.token;
  try {
    let res = await c2t.GetByQuery(
      {
        schoolname: sitecfg.schoolname,
        term: term,
        teachers: { name: userinfo.accountname, id: userinfo.userid },
      },
      null,
      {
        className: 1,
      }
    );
    ctx.body = {
      error: false,
      result: res,
    };
  } catch (err) {
    ctx.body = {
      error: true,
      message: err.message,
    };
  }
});

//获取指定班级当前参加基础课的人数
router.post("/getbaselessonstudents", async (ctx, next) => {
  let userinfo = ctx.request.token;
  let classname = ctx.request.body.classname;
  let month = ctx.request.body.month;
  try {
    let res = await application.GetByQuery(
      {
        $and: [
          { schoolname: sitecfg.schoolname },
          { term: term },
          { classno: classname },
          { status: { $ne: "退出" } },
          { baselesson: true },
          { month: month },
        ],
      },
      null,
      {
        stuname: 1,
      }
    );
    ctx.body = {
      error: false,
      result: res,
    };
  } catch (err) {
    ctx.body = {
      error: true,
      message: err.message,
    };
  }
});

//获取指定社团当前参加的人数
router.post("/getintereststudents", async (ctx, next) => {
  let userinfo = ctx.request.token;
  let classname = ctx.request.body.classname;
  let month = ctx.request.body.month;
  try {
    let res = await application.GetByQuery(
      {
        $and: [
          { schoolname: sitecfg.schoolname },
          { term: term },
          { selected: classname },
          { status: { $ne: "退出" } },
          { month: month },
        ],
      },
      null,
      {
        stuname: 1,
      }
    );
    ctx.body = {
      error: false,
      result: res,
    };
  } catch (err) {
    ctx.body = {
      error: true,
      message: err.message,
    };
  }
});

//以下代码作废，复制用

//获取站点基本信息
router.get("/getSiteBaseInfo", async (ctx, next) => {
  try {
    let query = [
      {
        $match: {
          schoolname: sitecfg.schoolname,
          term: term,
        },
      },
    ];
    let res = await siteBaseInfo.myAggregate(query, null, null);
    ctx.body = {
      error: false,
      result: res,
    };
  } catch (err) {
    ctx.body = {
      error: true,
      message: err.message,
    };
  }
});
//获取全校班级信息
router.get("/getAllClassName", async (ctx, next) => {
  try {
    let query = [
      {
        $match: {
          schoolname: sitecfg.schoolname,
          term: term,
        },
      },
      {
        $unwind: "$classno",
      },
      {
        $sort: {
          sq: 1,
        },
      },
    ];
    let res = await siteBaseInfo.myAggregate(query, null, null);
    ctx.body = {
      error: false,
      result: res,
    };
  } catch (err) {
    ctx.body = {
      error: true,
      message: err.message,
    };
  }
});

//获取全校教师
router.get("/getAllTeacher", async (ctx, next) => {
  try {
    let query = {
      accountType: "教师",
    };
    let fields = {
      username: 1,
      classno: 1,
      _id: 0,
    };
    let res = await base.myFindByQuery(query, fields);
    ctx.body = {
      error: false,
      result: res,
    };
  } catch (err) {
    ctx.body = {
      error: true,
      message: err.message,
    };
  }
});

//用户登录入口
// router.post('/login', async (ctx, next) => {
//     let un = ctx.request.body.username;
//     let up = Enpassword(ctx.request.body.password);
//     try {
//         var accts = await base.myAggregate([
//             {
//                 $match: {
//                     username:un,
//                     password2:up
//                 }
//             }, {
//                 $lookup: {
//                     from: "baseExtendInfo",
//                     localField: "_id",
//                     foreignField: "baseid",
//                     as: "baseExtendInfo"
//                 }
//             }, {
//                 $unwind: {path:"$baseExtendInfo",preserveNullAndEmptyArrays:true}
//             },  {
//                 $project: {
//                     _id: 1,
//                     username: 1,
//                     gender: 1,
//                     wxopenid: 1,
//                     accountType:1,
//                     "baseExtendInfo.班级":1
//                 }
//             }
//         ])
//         if (accts.length == 0) {
//             ctx.body = {
//                 "error": true,
//                 "message": "登录失败，请检查用户名和密码是否正确！"
//             }
//             return;
//         }
//         var userinfo = {
//             username: accts[0].username,
//             id: accts[0]._id,
//             admin: accts[0].accountType,
//             classno: accts[0].accountType=="学生"||accts[0].accountType=="毕业生" ? accts[0].baseExtendInfo.班级?accts[0].baseExtendInfo.班级 : '非学生用户':'非学生用户'
//         };
//         let token = jwt.sign(userinfo, "mxthink")
//         ctx.body = {
//             "error": false,
//             'userinfo': userinfo,
//             userotherinfo: accts[0],
//             "token": token
//         }
//     } catch (err) {
//         ctx.body = {
//             'error': true,
//             "message": "登录失败:" + err.message
//         }
//     }
// })

module.exports = router;
